import { Component, OnInit } from '@angular/core';
import { UserService } from '../../../../service/userService';
import { MessageService } from 'primeng/api';
import * as $ from 'jquery';
import {MyEncrypt} from '../../../../utils/my-encrypt';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {

  constructor(public userService:UserService, private msgService:MessageService) { }

  ngOnInit(): void {
  }

  UserRegister(){
    let username = <string>$("#username").val();
    let password = <string>$("#password").val();
    let nickname = <string>$("#nickname").val();
    if(username == ""){
      this.msgService.add({severity:'error', summary:'输入不合法', detail:"请输入用户名"});
      return;
    }
    if(password == ""){
      this.msgService.add({severity:'error', summary:'输入不合法', detail:"请输入密码"});
      return;
    }
    if(nickname == ""){
      this.msgService.add({severity:'error', summary:'输入不合法', detail:"请输入昵称"});
      return;
    }
    this.userService.UserRegister(username, MyEncrypt(password), nickname).subscribe(res=>{
      if(res == 'Existed'){
        this.msgService.add({severity:'error', summary:'用户名已存在', detail:"用户名"+username+"已存在"});
      }else if(res == 'Success'){
        this.msgService.add({severity:'success', summary:'注册成功', detail:"用户名"+username+"注册成功"});
      }
    })
  }
}
